package com.example.hao.broadcastreceiver_service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

public class MyService extends Service {



    private MusicReceiver musicReceiver;

    /**
     * 在服务内部 定义广播接收者
     */
    class MusicReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                int state = intent.getIntExtra("state", -1);
                switch (state) {
                    case 1:
                        playMusic();
                        break;
                    case 2:
                        pauseMusic();
                        break;
                    case 3:
                        nextSong();
                        break;
                }
            }
        }
    }


    private static final String TAG = MyService.class.getSimpleName();

    /**
     * 当 使用 bindService 启动服务的时候,该方法被调用
     *
     * @param intent
     * @return 返回 IBinder的引用对象(为了实现数据交互)
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind: intent " + intent);
        return new Binder();
    }


    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");


        /**
         * 在服务的 onCreate 注册广播接收者
         */
        musicReceiver = new MusicReceiver();
        IntentFilter intentFilter = new IntentFilter("a.b.c.music");
        registerReceiver(musicReceiver, intentFilter);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.w(TAG, "onStartCommand  intent: " + intent.getStringExtra("abc"));
        Log.w(TAG, "onStartCommand  flags: " + flags);
        Log.w(TAG, "onStartCommand  startId: " + startId);
//        return super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
        if (musicReceiver != null) {
            unregisterReceiver(musicReceiver);
        }
    }

    int i = 1;

    private void playMusic() {
        isPause = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (; i <= 100 && !isPause; i++) {
                    SystemClock.sleep(400);
                    Log.i(TAG,
                            "playMusic: 春哥在后台唱歌..." + i);
                    Intent intent = new Intent("x.y.z.progress");
                    intent.putExtra("p",i);
                    sendBroadcast(intent);
                }
            }
        }).start();
    }


    private boolean isPause = false;

    private void pauseMusic() {
        isPause = true;
        Log.i(TAG, "pauseMusic: 春哥暂停唱歌...");
    }

    private void nextSong() {
        Log.i(TAG, "pauseMusic: 春哥切歌了.......");
    }


}
